package com.ecjtu.stadiumre_servations.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ecjtu.stadiumre_servations.entity.domain.Student;
import com.ecjtu.stadiumre_servations.entity.dto.UserDTO;
import com.ecjtu.stadiumre_servations.mapper.StudentMapper;
import com.ecjtu.stadiumre_servations.mapper.UserMapper;
import com.ecjtu.stadiumre_servations.response.Result;
import com.ecjtu.stadiumre_servations.response.ResultCodeEnum;
import com.ecjtu.stadiumre_servations.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {
    @Autowired
    private StudentMapper studentMapper;
    @Resource
    private RedisTemplate redisTemplate;
    @Autowired
    private UserMapper userMapper;
    @Override
    public boolean isUserExistsInStudent(UserDTO userDTO) {
        Student student = getOne(Wrappers.<Student>lambdaQuery().eq(Student::getStudentId,userDTO.getUsername()));
        return student != null;
    }

    @Override
    public boolean isUserIdCardCorrect(UserDTO userDTO) {
        Student student = getOne(Wrappers.<Student>lambdaQuery().eq(Student::getStudentId,userDTO.getUsername()));
        return student.getIdCard().equals(userDTO.getIdCard());
    }

    @Override
    public Result studentList(Page<Student> page) {
//        // 生成缓存键，包含分页信息
//        String key = "student:list:page:" + page.getCurrent() + ":size:" + page.getSize();
//        // 尝试从Redis获取缓存的分页结果
//        Page<Student> cachedPage = (Page<Student>) redisTemplate.opsForValue().get(key);
//        if (cachedPage != null) {
//            // 缓存命中，直接返回
//            return Result.build(cachedPage, ResultCodeEnum.SELECT_SUCCESS);
//        }
//        // 缓存未命中，查询数据库
//        Page<Student> studentPage = studentMapper.selectPage(page, null);
//        // 将查询结果存入Redis，设置合理的过期时间（1天）
//        redisTemplate.opsForValue().set(key, studentPage, 1, TimeUnit.DAYS);
//        return Result.build(studentPage, ResultCodeEnum.SELECT_SUCCESS);
        return Result.build(studentMapper.selectPage(page,null),ResultCodeEnum.SELECT_SUCCESS);
    }

    @Override
    public Result addStudent(Student student) {
        LambdaQueryWrapper<Student>lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Student::getStudentId,student.getStudentId());
        Student student1 = studentMapper.selectOne(lambdaQueryWrapper);
        if (student1!=null){
            return Result.fail("已存在学号为"+student.getStudentId()+"的学生");
        }
        save(student);
        return Result.ok("学生信息添加成功");
    }
    @Override
    public Result removeStudent(String studentId){
        Student student = studentMapper.selectById(studentId);
        if (student==null){
            return Result.fail("不存在学号为"+studentId+"的学生");
        }
        studentMapper.deleteById(studentId);
        if (userMapper.selectById(studentId)!=null) {
            userMapper.deleteById(studentId);
        }
        return Result.ok("学生信息及其用户信息已删除");
    }

    public void updateStudentCache(Student student) {
        String key = "student:" + student.getStudentId();
        redisTemplate.opsForValue().set(key, student, 20, TimeUnit.MINUTES);
    }

}
